package com.aaa.stock.service.Impl;

import com.aaa.entity.*;
import com.aaa.stock.dao.*;
import com.aaa.stock.feign.UserFeign;
import com.aaa.stock.service.EditPurchaseService;
import com.aaa.stock.utils.IdWorker;
import com.aaa.stock.vo.EditPurchaseVo;
import com.aaa.stock.vo.PurchaseDto;
import com.aaa.stock.vo.PurchaseDtoVo;
import com.aaa.stock.vo.PurchaseItemDtos;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.ibatis.annotations.Delete;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Service
public class EditPurchaseServiceImpl extends ServiceImpl<EditPurchaseDao, PurchaseItem> implements EditPurchaseService {
    @Autowired
    private EditPurchaseDao editPurchaseDao;
    @Autowired
    private MedicinalDao medicinalDao;
    @Autowired
    private InventoryDao inventoryDao;

    @Autowired
    private PurchaseDao purchaseDao;

    @Autowired
    private PurchaseItemDao purchaseItemDao;

    @Autowired
    private UserFeign userFeign;

    @Override
    public Result<List<PurchaseItem>> getPurchaseItem(String purchaseId) {
        QueryWrapper<PurchaseItem> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(purchaseId)){
            queryWrapper.eq("purchase_id",purchaseId);
        }
        List<PurchaseItem> items = editPurchaseDao.selectList(queryWrapper);

        return new Result<>(200,"查询所有药品项目成功",items);
    }

    @Override
    public Result<IPage<Medicines>> findBytccAll(Integer currentPage, Integer pageSize, EditPurchaseVo editPurchaseVo) {
        IPage<Medicines> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Medicines> wrapper=new QueryWrapper<>();
        //关键词
        if (StringUtils.hasText(editPurchaseVo.getKeywords())){
            wrapper.like("keywords",editPurchaseVo.getKeywords());
        }
        //药品类型
        if (Objects.nonNull(editPurchaseVo.getMedicinesType())){
            wrapper.like("medicines_type",editPurchaseVo.getMedicinesType());
        }
        //处方类型
        if (Objects.nonNull(editPurchaseVo.getPrescriptionType())){
            wrapper.eq("prescription_type",editPurchaseVo.getPrescriptionType());
        }

        IPage<Medicines> page1 = medicinalDao.selectPage(page, wrapper);
        return new Result<IPage<Medicines>>(200,"查询用户成功",page1);

    }

    @Override
    public Result<String> purchaseIdRequest() {
        UUID uuid = UUID.randomUUID();

        String s = uuid.toString();
        String purchaseId = "CG" + s;
        return new Result<>(200,"成功生成订单号",purchaseId);
    }

    @Override
    public Result<List<PurchaseItem>> getPurchaseSh(String queryid) {
        QueryWrapper<PurchaseItem> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(queryid)){
            queryWrapper.eq("purchase_id",queryid);
        }
        List<PurchaseItem> items = editPurchaseDao.selectList(queryWrapper);
        for (PurchaseItem hhh: items) {
            InventoryLog inventoryLog=new InventoryLog();
            inventoryLog.setInventoryLogId(hhh.getItemId());
            inventoryLog.setPurchaseId(hhh.getPurchaseId());
            inventoryLog.setMedicinesId(hhh.getMedicinesId());
            inventoryLog.setInventoryLogNum(hhh.getPurchaseNumber());
            inventoryLog.setTradePrice(hhh.getTradePrice());
            inventoryLog.setPrescriptionPrice(hhh.getTradePrice());
            inventoryLog.setTradeTotalAmount(hhh.getTradeTotalAmount());
            inventoryLog.setBatchNumber(hhh.getBatchNumber());
            inventoryLog.setMedicinesName(hhh.getMedicinesName());
            inventoryLog.setMedicinesType(hhh.getMedicinesType());
            inventoryLog.setPrescriptionType(hhh.getPrescriptionType());
            inventoryLog.setProducterId(hhh.getProducterId());
            inventoryLog.setConversion(hhh.getConversion());
            inventoryLog.setUnit(hhh.getUnit());
            inventoryDao.insert(inventoryLog);
        }
//        Purchase purchase =new Purchase();
//        InventoryLog inventoryLog=new InventoryLog();
//        inventoryLog.getProviderId();
//        inventoryLog.getCreateTime();
//        inventoryLog.getCreateBy();

        return new Result<>(200,"查询所有药品项目成功",items);
    }

//    @Override
//    public Result<Boolean> GetAdd(PurchaseDtoVo purchaseVo) {
//        PurchaseDto purchaseDto=new PurchaseDto();
//        String purchaseId1 = purchaseDto.getPurchaseId();
//        int i = purchaseItemDao.deleteById(purchaseId1);
//
//        System.out.println("5555555555555555555555555");
//        System.out.println(i);



/*        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenInfo = JWTUtils.getInfo(token);
        String phone = tokenInfo.get("username").toString();
        User byUsername = userFeign.getByUsername(phone);
        IdWorker idWorker = new IdWorker();
        PurchaseDto purchaseDto = purchaseVo.getPurchaseDto();

        *//**
         * 生成两张表的采购单据ID
         *//*
        long l = idWorker.nextId();
        //生成两位数的随机数
        int random = ((int)Math.random())*90+10;
        //保证长度和数据库长度相同
        String purchaseId = "CG"+l+random;

        *//**
         * 添加采购项目详情表
         *//*
        List<PurchaseItemDtos> purchaseItems = purchaseVo.getPurchaseItemDtos();
        for (PurchaseItemDtos purchaseItem : purchaseItems) {
            PurchaseItem purchaseItem1 = new PurchaseItem();
            //生成雪花id
            long l1 = idWorker.nextId();
            //生成两位数的随机数
            String itemId = "" + l1+random;
            purchaseItem1.setItemId(itemId);
            purchaseItem1.setPurchaseId(purchaseId);
            purchaseItem1.setProducterId(purchaseItem.getProducterId());
            purchaseItem1.setTradeTotalAmount(purchaseDto.getPurchaseTradeTotalAmount());
            purchaseItem1.setMedicinesId(purchaseItem.getMedicinesId().toString());
            purchaseItem1.setTradePrice(purchaseItem.getTradePrice());
            purchaseItem1.setPurchaseNumber(purchaseItem.getPurchaseNumber());
            purchaseItem1.setConversion(purchaseItem.getConversion());
            purchaseItem1.setRemark(purchaseItem.getRemark());
            purchaseItem1.setMedicinesName(purchaseItem.getMedicinesName());
            purchaseItem1.setMedicinesType(purchaseItem.getMedicinesType());
            purchaseItem1.setPrescriptionType(purchaseItem.getPrescriptionType());
            purchaseItem1.setBatchNumber(purchaseItem.getBatchNumber());
            purchaseItem1.setUnit(purchaseItem.getUnit());
            purchaseItem1.setKeywords(purchaseItem.getKeywords());
            purchaseItemDao.insert(purchaseItem1);
        }

        *//**
         * 添加单据表
         *//*

        Purchase purchase =new Purchase();
        purchase.setPurchaseId(purchaseId);
        purchase.setProviderId(purchaseDto.getProviderId());
        purchase.setPurchaseTradeTotalAmount(purchaseDto.getPurchaseTradeTotalAmount());
        purchase.setStatus("2");
        purchase.setApplyUserName(byUsername.getUserName());
        purchase.setCreateBy(byUsername.getUserName());
        purchase.setCreateTime(LocalDateTime.now());
        int insert = purchaseDao.insert(purchase);
        return new Result<Boolean>(200,"添加成功",insert>0?true:false);*/
//        return new Result<Boolean>(200,"添加成功");
//    }
}

